package com.apex.aolonfitdemo.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.apex.aolonfit.AolonFitManager;
import com.apex.aolonfitdemo.R;
import com.apex.aolonfitdemo.databinding.ActivityUserInfoBinding;
import com.apex.aolonfitdemo.dialog.AgeDialog;
import com.apex.aolonfitdemo.dialog.HeightDialog;
import com.apex.aolonfitdemo.dialog.SexDialog;
import com.apex.aolonfitdemo.dialog.WaitingDialog;
import com.apex.aolonfitdemo.dialog.WeightDialog;
import com.apex.ax_bluetooth.callback.GeneralCallback;
import com.apex.ax_bluetooth.callback.PersonInfoCallback;
import com.apex.ax_bluetooth.core.EABleManager;
import com.apex.ax_bluetooth.enumeration.EABleConnectState;
import com.apex.ax_bluetooth.enumeration.PersonHand;
import com.apex.ax_bluetooth.enumeration.QueryWatchInfoType;
import com.apex.ax_bluetooth.model.EABlePersonInfo;
import com.apex.ax_bluetooth.utils.LogUtils;
import com.cyber.bridge.module.bean.UserProfileSetEntity;
import com.cyber.bridge.module.listener.OnCyberQueryAndSetStateListener;

public class UserInfoActivity extends AppCompatActivity {
    private final String TAG = this.getClass().getSimpleName();
    private WaitingDialog waitingDialog;

    private SexDialog sexDialog;
    private String tempSex;
    private AgeDialog ageDialog;
    private int tempAge;
    private HeightDialog heightDialog;
    private int tempHeight;
    private WeightDialog weightDialog;
    private int tempWeight;


    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x40) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                UserProfileSetEntity userProfileSetEntity = (UserProfileSetEntity) msg.obj;
                if (userProfileSetEntity == null) {
                    return;
                }
                tempSex = userProfileSetEntity.getGender() == 0 ? getString(R.string.female) : getString(R.string.male);
                tempAge = userProfileSetEntity.getAge();
                tempHeight = userProfileSetEntity.getHeight();
                tempWeight = userProfileSetEntity.getWeight();
                activityUserInfoBinding.userSex.setText(tempSex);
                activityUserInfoBinding.userAge.setText(tempAge + "");
                activityUserInfoBinding.userHeight.setText(tempHeight + "");
                activityUserInfoBinding.userWeight.setText(tempWeight + "");

            } else if (msg.what == 0x42) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                activityUserInfoBinding.userSex.setText(tempSex);
                activityUserInfoBinding.userAge.setText(tempAge + "");
                activityUserInfoBinding.userHeight.setText(tempHeight + "");
                activityUserInfoBinding.userWeight.setText(tempWeight + "");

            } else if (msg.what == 0x43) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(UserInfoActivity.this, getString(R.string.modification_failed), Toast.LENGTH_SHORT).show();
            }
        }
    };
    ActivityUserInfoBinding activityUserInfoBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityUserInfoBinding = ActivityUserInfoBinding.inflate(getLayoutInflater());
        setContentView(activityUserInfoBinding.getRoot());
        activityUserInfoBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activityUserInfoBinding.tool);
        getSupportActionBar().setTitle(null);
        activityUserInfoBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        AolonFitManager.getInstance().setOnUserInfoListener(new OnCyberQueryAndSetStateListener<UserProfileSetEntity>() {
            @Override
            public void queryConfig(UserProfileSetEntity userProfileSetEntity) {
                if (mHandler != null) {
                    Message message = mHandler.obtainMessage();
                    message.what = 0x40;
                    message.obj = userProfileSetEntity;
                    mHandler.sendMessage(message);
                }
            }

            @Override
            public void setConfig(boolean b) {
                if (mHandler != null) {
                    mHandler.sendEmptyMessage(b ? 0x42 : 0x43);
                }
            }
        });
        if (waitingDialog == null) {
            waitingDialog = new WaitingDialog(UserInfoActivity.this);
        }
        waitingDialog.show();
        AolonFitManager.getInstance().queryUserInfo();
        activityUserInfoBinding.userSex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sexDialog == null) {
                    sexDialog = new SexDialog(UserInfoActivity.this);
                    sexDialog.setSelectListener(new SexDialog.SelectListener() {
                        @Override
                        public void selectData(String sex) {
                            tempSex = sex;
                            int userSex = 0;
                            if (sex.equalsIgnoreCase(getString(R.string.male))) {
                                userSex = 1;
                            }
                            if (waitingDialog == null) {
                                waitingDialog = new WaitingDialog(UserInfoActivity.this);
                            }
                            if (!waitingDialog.isShowing()) {
                                waitingDialog.show();
                            }
                            AolonFitManager.getInstance().setUserInfo(new UserProfileSetEntity(tempAge, userSex, tempHeight, tempWeight));

                        }
                    });
                }
                if (!sexDialog.isShowing()) {
                    LogUtils.i(TAG, "显示对话框");
                    sexDialog.show();
                }

            }
        });
        activityUserInfoBinding.userAge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ageDialog == null) {
                    ageDialog = new AgeDialog(UserInfoActivity.this);
                    ageDialog.setSelectListener(new AgeDialog.SelectListener() {
                        @Override
                        public void selectData(int age) {
                            tempAge = age;

                            if (waitingDialog == null) {
                                waitingDialog = new WaitingDialog(UserInfoActivity.this);
                            }
                            if (!waitingDialog.isShowing()) {
                                waitingDialog.show();
                            }
                            AolonFitManager.getInstance().setUserInfo(new UserProfileSetEntity(tempAge, getString(R.string.male).equalsIgnoreCase(tempSex) ? 1 : 0, tempHeight, tempWeight));

                        }
                    });
                }
                if (!ageDialog.isShowing()) {
                    ageDialog.show();
                }

            }
        });
        activityUserInfoBinding.userHeight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (heightDialog == null) {
                    heightDialog = new HeightDialog(UserInfoActivity.this);
                    heightDialog.setSelectListener(new HeightDialog.SelectListener() {
                        @Override
                        public void selectData(int age) {
                            tempHeight = age;

                            if (waitingDialog == null) {
                                waitingDialog = new WaitingDialog(UserInfoActivity.this);
                            }
                            if (!waitingDialog.isShowing()) {
                                waitingDialog.show();
                            }
                            AolonFitManager.getInstance().setUserInfo(new UserProfileSetEntity(tempAge, getString(R.string.male).equalsIgnoreCase(tempSex) ? 1 : 0, tempHeight, tempWeight));
                        }
                    });
                }
                if (!heightDialog.isShowing()) {
                    heightDialog.show();
                }
            }
        });
        activityUserInfoBinding.userWeight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (weightDialog == null) {
                    weightDialog = new WeightDialog(UserInfoActivity.this);
                    weightDialog.setSelectListener(new WeightDialog.SelectListener() {
                        @Override
                        public void selectData(int age) {

                            tempWeight = age;
                            if (waitingDialog == null) {
                                waitingDialog = new WaitingDialog(UserInfoActivity.this);
                            }
                            if (!waitingDialog.isShowing()) {
                                waitingDialog.show();
                            }
                            AolonFitManager.getInstance().setUserInfo(new UserProfileSetEntity(tempAge, getString(R.string.male).equalsIgnoreCase(tempSex) ? 1 : 0, tempHeight, tempWeight));

                        }
                    });
                }
                if (!weightDialog.isShowing()) {
                    weightDialog.show();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {

        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }

        if (sexDialog != null) {
            sexDialog.dismiss();
            sexDialog.destroyDialog();
            sexDialog = null;
        }
        if (ageDialog != null) {
            ageDialog.dismiss();
            ageDialog.destroyDialog();
            ageDialog = null;
        }
        if (heightDialog != null) {
            heightDialog.dismiss();
            heightDialog.destroyDialog();
            heightDialog = null;
        }
        if (weightDialog != null) {
            weightDialog.dismiss();
            weightDialog.destroyDialog();
            weightDialog = null;
        }
        super.onDestroy();
    }
}
